# 开发模式
"""
开发模式：
    单例模式: 对内存的优化
"""


class Student:
    pass


class Singleton:
    # 私有化类属性,单例的地址存在这个属性
    __instance = None
    name = 'Java'

    # 重写父类：
    def __new__(cls, *args, **kwargs):
        print("--new--")
        if cls.__instance is None:
            cls.__instance = object.__new__(cls)
        return cls.__instance  # 扔给底层的__init__

    def show(self, n):
        print("--------->", Singleton.name, n)


s1 = Student()
print(s1)  # <__main__.Student object at 0x0000018FAD915FC8>
s2 = Student()
print(s2)  # <__main__.Student object at 0x0000018FAD915F48>

st1 = Singleton()
print(st1)  # <__main__.Singleton object at 0x0000023FAB56F0C8>
st1.show(5)

st2 = Singleton()
print(st2)  # <__main__.Singleton object at 0x0000023FAB56F0C8>
st2.show(7)

print(dir(Singleton))
